package com.example.himalaya.view.adapters;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.himalaya.R;
import com.squareup.picasso.Picasso;
import com.ximalaya.ting.android.opensdk.model.album.Album;

import java.util.ArrayList;
import java.util.List;

public class RecommendListAdapter extends RecyclerView.Adapter<RecommendListAdapter.InnerHolder> {

    // Variable
    private static final String TAG = "RecommendListAdapter";

    private List<Album> mData = new ArrayList<>();
    private onRecommendItemClickListener mItemClickListener = null;


    // 创建显示内容的Item
    @NonNull
    @Override
    public RecommendListAdapter.InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 这里是载view
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_album, parent, false);
        return new InnerHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull RecommendListAdapter.InnerHolder holder, final int position) {
        // 这里是设置数据
        // 将位置下标赋给view的tag
        holder.itemView.setTag(position);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (mItemClickListener != null) {
                    mItemClickListener.onItemClick((Integer) view.getTag(), mData.get(position));
                }
                Log.d(TAG, "holder.itemView click ->" + view.getTag());
            }
        });

        holder.setData(mData.get(position));
    }

    @Override
    public int getItemCount() {
        // 返回要显示的个数
        if (mData != null) {
            return mData.size();
        }

        return 0;
    }


    // 将从Hemalaya's 的数据放到适配器
    public void setData(List<Album> albumList) {
        if (mData != null) {
            mData.clear();              // 清空
            mData.addAll(albumList);    // 装入新数据
        }


        // 跟新UI
        notifyDataSetChanged();
    }

    public class InnerHolder extends RecyclerView.ViewHolder {
        public InnerHolder(View view) {
            super(view);
        }

        public void setData(Album album) {
            // 获取对象
            // 封面
            ImageView albumCover = itemView.findViewById(R.id.album_cover);

            // title
            TextView albumTitle = itemView.findViewById(R.id.album_title);

            // 描述
            TextView albumDesrc = itemView.findViewById(R.id.album_discription_tv);

            // 播放数量
            TextView albumPlayCount = itemView.findViewById(R.id.album_play_count);

            // 专辑内容数量
            TextView albumContentCount = itemView.findViewById(R.id.album_content_size);


            // 设置数据

            albumTitle.setText(album.getAlbumTitle());
            albumDesrc.setText(album.getAlbumIntro());
            albumPlayCount.setText(album.getPlayCount() + "");
            albumContentCount.setText(album.getIncludeTrackCount() + "");

            String coverUrl = album.getCoverUrlLarge();

            if (!TextUtils.isEmpty(coverUrl)) {
                Picasso.with(itemView.getContext()).load(coverUrl).into(albumCover);
            } else {
                albumCover.setImageResource(R.mipmap.default_picture);
            }

        }
    }

    public void setOnRecommendItemClickListener(onRecommendItemClickListener listener) {
        this.mItemClickListener = listener;
    }

    public interface onRecommendItemClickListener {
        void onItemClick(int position, Album album);
    }
}
